(function(){
    function GetUnTicket(){
        this.init();
     
       
        
        

    }

    GetUnTicket.prototype = {
        constructor:GetUnTicket,
        init:function(){
           
            var _this = this;
           

            // 日历
            $.datetimepicker.setLocale('ch');
            $('#endtime').datetimepicker({
                format: 'Y-m-d',
                value:new Date().dateadd("d"),
                maxDate: new Date(),
                onChangeDateTime:function(dp,$input){
                    var limitStart = new Date($('#begintime').val().replace('-','/'));
                    var nowStart = new Date($('#endtime').val().replace('-','/'));
                    if(limitStart > nowStart){
                        $('#endtime').val(limitStart.format("YY-M-D"))

                    }
                },
                timepicker: false
            });
            
            $('#begintime').datetimepicker({
                format: 'Y-m-d',
                value:new Date().dateadd("d", -2),
                maxDate: new Date(),
                onChangeDateTime:function(dp,$input){
                    var limitStart = new Date($('#endtime').val().replace('-','/'));
                    var nowStart = new Date($('#begintime').val().replace('-','/'));
                    if(limitStart < nowStart){
                        $('#begintime').val(limitStart.format("YY-M-D"))

                    }
                    
                },
                timepicker: false
            });
            // 中奖输入框控制
            $("#selzjLabel").unbind("click").click(function(){
                 var isChecked = $("#ischecked").prop("checked");
                 if( !isChecked )
                 {
                     $("#djiangMoney").prop("disabled", false);
                 }
                 else
                 {
                    $("#djiangMoney").prop("disabled", true);

                 }
            });
            $("#ticketStatus").change(function(){
                if( this.value == "5")
                {
                    $("#drawtimetip").show();
                }
                else
                {
                    $("#drawtimetip").hide();
                }


            });
            // 输入框限制
            $("#djiangMoney").on("input", function(){
                this.value = this.value.replace(/[^\d.]/,"");
                this.value = this.value.replace(/^\./g,"");
                this.value = this.value.replace(".","$#$").replace(/\./g, "").replace("$#$",".");
            });



            // 报表
            this.uploaddownReport();
           
            this.getUnticketList(1);

              // 统计信息
              _this.getTicketCountInfo();

            // 店主接单情况
            _this.storeChupiaoInfo();

            
            $.extend({page:function(pn){
                _this.getUnticketList(pn);
            }});
            $("#searchBtn").unbind("click").click(function(){
                _this.getUnticketList(1);
                // 店主接单情况
                _this.storeChupiaoInfo();

                _this.getTicketCountInfo();
            });

        

        },
      
        getUnticketList:function( pn ){
            var _this = this;
            var stateSel = $("#ticketStatus").val();
            var isWin = $("#ischecked").prop("checked");
            var isDajiang = $("#djiangMoney").val();
            var gameid = $("#gameId").val();
            var wmoney = "";
            if( stateSel == 5 )//兑奖
            {
                wmoney = "&wmoney=1";
            }

            if( isWin)
            {
                wmoney =  !isDajiang ? "&wmoney=8000" : "&wmoney="+ isDajiang;
            }

            var state =  stateSel;
            var stime = $("#begintime").val();
            var etime = $("#endtime").val();
            var ticketOrder = $("#ticketOrder").val().trim();
            var orderNums = $("#orderNums").val().trim();
            var siteid = $("#siteId").val().trim();
            var time = "";
            stime = stime ? "&stime="+ stime + " 00:00:00" : "";
            etime = etime ? "&etime="+ etime + " 23:59:59" : "";
            // if( stateSel == "3")
            // {
            //     time = "&time=out_time";
            // }
            // else if( stateSel == "5" )
            // {
            //     time = "&time=draw_time";
            // }
             // 投注号码搜索
             var tzcdesc = $("#touzhucdesc").val();
             tzcdesc = tzcdesc?"&cdesc="+tzcdesc:'';
            //  期次搜索
            var buyperoid = $("#buyperoid").val();
            buyperoid = buyperoid?"&period="+buyperoid:'';

           

            ticketOrder = ticketOrder ? "&ticketid="+ticketOrder : "";
            orderNums = orderNums ? "&orderid="+orderNums : "";
            siteid = siteid ? "&siteid="+siteid : "";
            var nodata = '';
            var sort = '&sort=2';
            if( stateSel == "12")
            {
                nodata = '<tr><td  colspan="12">暂无信息</td></tr>';
                $("#specialTeat, #selectBtn").show();
                sort = '&sort=1';//未出票 sort 1 
                stime = "";
                etime = "";
            }
            else
            {
                nodata = '<tr><td  colspan="15">暂无信息</td></tr>';
                $("#specialTeat, #selectBtn").hide();

            }

            if(ticketOrder || orderNums)
            {
                stime = "";
                etime = "";
                $("#begintime").val("");
                $("#endtime").val("");
            }

            var tableStr = _this.setTableHead();

            $("#reportTable").html(tableStr);
            $.ajax({
                type:"get",
                url:"/sys?fid=101&type=8&gameid="+gameid+"&time=out_time&state="+state+sort+buyperoid+tzcdesc+wmoney+ticketOrder+orderNums+siteid+stime+etime+time+"&ps=15&pn="+pn,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code == 1 )
                    {
                        var data = info.data;
                        var count = info.count;
                        var stateArr = ["已撤单","未分配","已分配","已接单","已出票","已计奖","已兑奖"];
                        if( data.length )
                        {
                            var strArr = [];
                            data.forEach(function( value, key  ){
                                var ticketid = value.ticketid;
                                var orderid = value.orderid;
                                var ctime = value.createtime.substring(5,19);
                                var etime = value.endtime.substring(5,19);
                                var siteid = value.siteid ? value.siteid : "";
                                var stateStatus = value.state;//0 未分配 可以撤单
                                var stateIndex = value.state + 1;
                                var state = stateArr[stateIndex];
                                var mulity = value.mulity;
                                var tmoney = value.tmoney;
                                var codes = value.codes;
                                var taketime = value.fenpeitime.substring(5,19);
                                var drawtime = value.drawtime.substring(5,19);
                                if( stateStatus <= 0 )
                                {
                                    taketime = "";
                                }

                                var outtime = value.outtime ? value.outtime.substring(5,19) : "--";//出票时间
                                var wmoney = stateSel == "12" ? "--" : value.wmoney ;//兑奖金额
                                var sysmoney = stateSel == "12"  ? "--" : value.sysmoney;//计奖金额
                                var selStr = '';
      
                                if( stateSel == "12" )
                                {
                                    if( stateStatus == 0 || stateStatus == 1 || stateStatus == 2 )
                                    {
                                        selStr = '<div class="checkbox selectcheckbox"><label><input type="checkbox" class="flat   selectflat"    value="'+ticketid+'"  data-status="'+stateStatus+'"></label></div>';
                                       
                                    }
                                    

                                }

                                var trClass =  key%2 == 0 ? "even" : "odd";
                                var html = '<tr class="'+trClass+'"  id="tr'+ticketid+'">\
                                             <td>'+selStr+'</td>\
                                               <td  class="ticketidtd   add-at"      style="mso-number-format:\"@\";"><a  href="ticketinfo.html?type=8&tid='+ticketid+'"  target="_blank">'+ticketid+'</a></td>\
                                                <td  class="add-at" >'+orderid+' </td>\
                                                <td  >'+ctime+'</td>\
                                                <td>'+etime+'</td>';
                                               

                            if( stateSel == "12")
                            {
                                html += '<td>'+taketime+'</td>';
                                html += '<td>'+siteid+'</td>\
                                        <td>'+state+'</td>\
                                        <td>'+mulity+'</td>\
                                        <td>'+tmoney+'</td>\
                                        <td>'+codes+'</td>';

                            }
                            else
                            {

                                html += '<td>'+outtime+'</td>';

                                if( stateSel == "5")//兑奖
                                {
                                    html += '<td>'+drawtime+'</td>';
                                }
                                else
                                {
                                    html += '<td>'+taketime+'</td>';

                                }

       
                                html +='<td>'+siteid+'</td>\
                                        <td>'+state+'</td>\
                                        <td>'+mulity+'</td>\
                                        <td>'+tmoney+'</td>\
                                        <td>'+wmoney+'</td>\
                                        <td>'+sysmoney+'</td>\
                                        <td>'+codes+'</td>';

                            }


                    
                                             
                               

                                  if( stateStatus == 0 || stateStatus == 1 || stateStatus == 2 )
                                  {
                                      html += '  <td><a  href="javascript:;"  class="btn btn-info  cancelBtn"  data-tid="'+ticketid+'"  data-status="'+stateStatus+'">撤单</a></td>';
                                     
                                  }
                                  else
                                  {
                                     html += '<td>---</td>';
                                  }

                                  html += '</tr>';

                                    strArr.push( html );

                            });

                            $("#unticketList").html( strArr.join(""));
                           

                            // 导出表格
                            $("#exportReport").unbind("click").click(function(){
                                $("#reportTable").tableExport({type:"excel",  escape:"false"});
                              

                            });


                           

                            

                            // 撤单
                            $(".cancelBtn").unbind("click").click(function(){
                                var tid = $(this).data("tid");
                                _this.cancelStatus = $(this).data("status");
                                $("#hiddenTid").val(tid);
                                $("#cancelModal").modal();

                            });
                            $("#confirmBtn").unbind("click").click(function(){
                                _this.cancelTicket();
                            });

                            // 优先出票
                            $("#specialTeat").unbind("click").click(function(){
                                if( stateSel == "12" )
                                {
                                    var selTicketArr = [];
                                    $(".selectflat:checked").each(function(){
                                         var tid = $(this).val();
                                         selTicketArr.push(tid);
                                    });
      
                                    if( selTicketArr.length )
                                    {
                                        $("#headTicketsModal").modal();
                                    }
                                    else
                                    {
                                        $("#myModalLabel").html("请先选择需要优先出票的订单");
                                        $("#loginoutModal").modal();
    
                                    }
                                }
                               
                               
                            });

                            $("#confirmheadBtn").unbind("click").click(function(){
                                _this.aheadTickets();
                            });
                            // 批量撤单

                            
                         $("#selectLabel").click(function(){
                                
                                var isChecked = $("#selectAllChecbox").prop("checked");
                      

                                if( isChecked )
                                {
                                    $(".selectflat").prop("checked", true);
                                }
                                else
                                {
                                    $(".selectflat").prop("checked", false);

                                }

                            });
                            $("#selectBtn").unbind("click").click(function(){
                                if( stateSel == "12" )
                                {
                                    var selTicketArr = [];
                                    $(".selectflat:checked").each(function( Index ){
                                         var tid = $(this).val();
                                         selTicketArr.push(tid);
                                         if( Index==0)
                                         {
                                            _this.cancelStatus = $(this).data("status");
                                         }
                                        
                                    });
      
                                    if( selTicketArr.length )
                                    {
                                        $("#cancelMutiple").modal();
                                    }
                                    else
                                    {
                                        $("#myModalLabel").html("请先选择需要批量撤销的订单");
                                        $("#loginoutModal").modal();
    
                                    }
                                }
                               
                               
                            });
                            $("#confirmMultipleBtn").unbind("click").click(function(){
                                _this.cancelLotsTicket();
                            });




                            




                            // 分页
                            var pn = count.pn;
                            var tp = count.tp;
                            var tr = count.tr;
                            $("#countpages").html( tp );
                            $("#countnums").html( tr );
        
                            var pageHtml = getPages(pn,tp, "$.page");

                            $("#paginationSel").html(pageHtml);



                        }
                        else
                        {
                            $("#paginationSel").html("");
                            $("#unticketList").html(nodata);
                            var pn = count.pn;
                            var tp = count.tp;
                            var tr = count.tr;
                            $("#countpages").html( tp );
                            $("#countnums").html( tr );

                        }

                    }
                    else
                    {
                        if( code == 2 )
                        {
                            location.href = "login.html";

                        }
                        else
                        {
                            $("#tipsContent").html( desc );
                            $("#loginoutModal").modal();

                        }
                    }

   


                }
            });


        },
        // 撤单
        cancelTicket:function(){
            var tid = $("#hiddenTid").val();
            var gameid = $("#gameId").val();
            var _this = this;
            $.ajax({
                type:"get",
                url:"/sys?fid=102&type=8&ticketid="+tid+"&state="+ _this.cancelStatus,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code == 1 )
                    {
                        $("#tr"+tid).remove();
                        $("#cancelModal").modal("hide");
                    }
                    else
                    {
                        $("#cancelModal").modal("hide");
                        if( code == 2 )
                        {
                            $("#myModalLabel").html("你还未登录");
                            $("#loginoutModal").modal();
                            setTimeout(function(){
                                location.href = "login.html";
                            }, 1000);
                        }
                        else
                        {
                            $("#myModalLabel").html(desc);
                            $("#loginoutModal").modal();

                        }

                    }


                }
            });


        },
        // 批量撤单
        cancelLotsTicket:function(){
            var _this = this;
            var selTicketArr = [];
            $(".selectflat:checked").each(function(){
                 var tid = $(this).val();
                 selTicketArr.push(tid);
            });

            var selTickets = selTicketArr.join(","); 
            var gametype = $("#gameId").val();
            $.ajax({
                type:"get",
                url:"/sys?fid=102&type=8&ticketid="+selTickets+"&state="+ _this.cancelStatus,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code == 1 )
                    {
                        selTicketArr.forEach(function( item ){
                            $("#tr"+item).remove();
                        });

                        $("#cancelMutiple").modal("hide");
                        $("#myModalLabel").html("批量撤单成功!");
                        $("#loginoutModal").modal();
                    }
                    else
                    {
                        $("#cancelMutiple").modal("hide");
                        if( code == 2 )
                        {
                            $("#myModalLabel").html("你还未登录");
                            $("#loginoutModal").modal();
                            setTimeout(function(){
                                location.href = "login.html";
                            }, 1000);
                        }
                        else
                        {
                            $("#myModalLabel").html(desc);
                            $("#loginoutModal").modal();

                        }

                    }


                }
            });


        },
        // 优先出票
        aheadTickets:function(){
            var selTicketArr = [];
            $(".selectflat:checked").each(function(){
                 var tid = $(this).val();
                 selTicketArr.push(tid);
            });

            var selTickets = selTicketArr.join(",");
            var gametype = $("#gameId").val();
            $.ajax({
                type:"get",
                url:"/sys?fid=105&type=8&ticketid="+selTickets,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code == 1 )
                    {
                        var count = info.count;
                        $("#headTicketsModal").modal("hide");
                        $("#myModalLabel").html(count+"条订单优先出票,设置成功");
                        $("#loginoutModal").modal();
                    }
                    else
                    {
                        if( code == 2 )
                        {
                            $("#myModalLabel").html("你还未登录");
                            $("#loginoutModal").modal();
                            setTimeout(function(){
                                location.href = "login.html";
                            }, 1000);
                        }
                        else
                        {
                            $("#myModalLabel").html(desc);
                            $("#loginoutModal").modal();

                        }

                    }

                }

            });

            
        },
        // 报表
        uploaddownReport:function(){
            $("#chupiaoReport").click(function(){
                 var stime = $("#begintime").val();
                 var etime = $("#endtime").val();
                 stime = stime ? "&stime="+ stime + " 00:00:00" : "";
                 etime = etime ? "&etime="+ etime + " 23:59:59" : "";
                 var siteid = $("#siteId").val().trim();
                 siteid = siteid ? "&siteid="+siteid : "";
                 var gameid = $("#gameId").val();
         
                 location.href=  "/sys?fid=306&type=8&gameid="+gameid+""+siteid+"&state=345"+stime+etime+"&time=out_time";
            });

            $("#rewardReport").click(function(){
                var stime = $("#begintime").val();
                var etime = $("#endtime").val();
                stime = stime ? "&stime="+ stime + "  00:00:00" : "";
                etime = etime ? "&etime="+ etime + " 23:59:59" : "";
                var siteid = $("#siteId").val().trim();
                siteid = siteid ? "&siteid="+siteid : "";
                var gameid = $("#gameId").val();
                location.href=  "/sys?fid=306&type=8&gameid="+gameid+""+siteid+"&state=5"+stime+etime+"&time=draw_time";
           });



        },
        // 票数统计
        getTicketCountInfo:function(){
            var gametype = $("#gameId").val();
            $.ajax({
                type:"get",
                url:"/sys?fid=401&type=8",
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    if( code == 1 )
                    {
                        var data = info.data;
                        if( data.length )
                        {
                            var  strArr = [];
                            data.forEach(function( value ){
                                var etime = value.endtime;
                                var mulity = value.mulity;
                                var html = ' <tr>\
                                                <td>'+etime+'</td>\
                                                <td>'+mulity+'</td>\
                                            </tr>';

                                    strArr.push( html );

                            });
                            $("#ticketsCount").html(strArr.join(""));


                        }
                        else
                        {
                            $("#ticketsCount").html('<tr><td colspan="2">暂无统计信息</td></tr>');
                        }
                    }

                }
            });

        },
        // 店主接单情况
        storeChupiaoInfo:function(){
            $.ajax({
                type:"get",
                url:"/sys?fid=307",
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    if( code == 1 )
                    {
                        var data = info.data;
                        if( data )
                        {
                            var kongxians = data.online, busys = data.jiedan;
                           
                            $("#leisureNums").html(kongxians);
                            $("#busyNums").html(busys);
                           
                        }

                    }


                }
            });


        },
          // 
       setTableHead:function(){
            var status = $("#ticketStatus").val();
            var tablehead = '';
            if( status == "12")//出票中
            { //10
                 tablehead = ' <colgroup>\
                                <col  width="3%" >\
                                <col  width="9%">\
                                <col  width="6%">\
                                <col  width="8%">\
                                <col  width="8%">\
                                <col  width="8%">\
                                <col  width="6%">\
                                <col  width="5%">\
                                <col  width="5%">\
                                <col  width="5%">\
                                <col  width="14%">\
                                <col  width="4%">\
                            </colgroup>';

            tablehead += ' <thead>\
                                <tr class="headings">\
                                   <th class="column-title  totalchecboxwrap">\ <div class="checkbox" >\
                                            <label   id="selectLabel">\
                                                <div class="icheckbox_flat-green" style="position: relative;"><input type="checkbox" class="flat" style="display: none; position: absolute; opacity: 0;" id="selectAllChecbox"><ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: rgb(255, 255, 255); border: 0px; opacity: 0;"></ins></div></label></div></th>\
                                    <th class="column-title">票号 </th>\
                                    <th class="column-title">订单编号</th>\
                                    <th class="column-title">投注时间</th>\
                                    <th class="column-title">截止时间</th>\
                                    <th class="column-title">分配时间</th>\
                                    <th class="column-title">机器编号</th>\
                                    <th class="column-title">出票状态</th>\
                                    <th class="column-title">投注倍数</th>\
                                    <th class="column-title">投注金额</th>\
                                    <th class="column-title">投注内容</th>\
                                    <th class="column-title">是否撤单</th>\
                                </tr>\
                            </thead>';


                

            }
            else
            {
                tablehead = ' <colgroup>\
                                <col  width="3%">\
                                <col  width="9%">\
                                <col  width="6%">\
                                <col  width="8%">\
                                <col  width="8%">\
                                <col  width="8%">\
                                <col  width="8%">\
                                <col  width="8%">\
                                <col  width="5%">\
                                <col  width="5%">\
                                <col  width="5%">\
                                <col  width="5%">\
                                <col  width="5%">\
                                <col  width="13%">\
                                <col  width="4%">\
                            </colgroup>';

                            tablehead += ' <thead>\
                            <tr class="headings">\
                               <th class="column-title  totalchecboxwrap">\ <div class="checkbox" >\
                                        <label   id="selectLabel">\
                                            <div class="icheckbox_flat-green" style="position: relative;"><input type="checkbox" class="flat" style="display: none; position: absolute; opacity: 0;" id="selectAllChecbox"><ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: rgb(255, 255, 255); border: 0px; opacity: 0;"></ins></div></label></div></th>\
                                <th class="column-title">票号 </th>\
                                <th class="column-title">订单编号</th>\
                                <th class="column-title">投注时间</th>\
                                <th class="column-title">截止时间</th>\
                                <th class="column-title">出票时间</th>';

                                if( status == "5")
                                {
                                    tablehead += ' <th class="column-title">兑奖时间</th>';
                                }
                                else
                                {
                                    tablehead += ' <th class="column-title">分配时间</th>';
                                }

                             tablehead += '<th class="column-title">机器编号</th>\
                                <th class="column-title">出票状态</th>\
                                <th class="column-title">投注倍数</th>\
                                <th class="column-title">投注金额</th>\
                                <th class="column-title">兑奖金额</th>\
                                <th class="column-title">计奖金额</th>\
                                <th class="column-title">投注内容</th>\
                                <th class="column-title">是否撤单</th>\
                            </tr>\
                        </thead>';
            }

            tablehead += ' <tbody   id="unticketList"></tbody>';

            return  tablehead;



            



        }

      
      
    };





  new GetUnTicket();

}());